package com.trinhrae.shooting.config;

import com.trinh.utils.crypto.config.KeyConfig;
import com.trinh.utils.crypto.cryptors.AES256Util;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Author: TrinhRae
 * @Package: com.trinhrae.shooting.demos.config
 * @Project: shooting
 * @Date: 2025/8/29 11:51
 */
@Configuration
public class Configure {

    @Bean
    public AES256Util getAES() {
        return new AES256Util(new KeyConfig());
    }

    /**
     * 注册泛型为String, Object的RedisTemplate.
     * SpringBoot默认是泛型Object, Object的RedisTemplate.
     *
     * @param factory
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // 设置 key 的序列化方式
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        // 设置 value 的序列化方式
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        template.afterPropertiesSet();
        return template;
    }
}
